export function wgs84togcj02(wgs_lon, wgs_lat)
{
    if (out_of_china(wgs_lon, wgs_lat))
    {
        //不在国内
        let back = [ wgs_lon, wgs_lat ];
        return back;
    }
    else
    {
       var dwgs_lon = transformlon(wgs_lon - 105.0, wgs_lat - 35.0);
       var dwgs_lat = transformlat(wgs_lon - 105.0, wgs_lat - 35.0);
       var radwgs_lat = wgs_lat / 180.0 * Math.PI;
       var magic = Math.sin(radwgs_lat);
       var magic = 1 - ee * magic * magic;
       var sqrtmagic = Math.sqrt(magic);
       var dwgs_lon = (dwgs_lon * 180.0) / (a / sqrtmagic * Math.cos(radwgs_lat) * Math.PI);
       var dwgs_lat = (dwgs_lat * 180.0) / ((a * (1 - ee)) / (magic * sqrtmagic) * Math.PI);
       var gcj_lon = wgs_lon + dwgs_lon;
       var gcj_lat = wgs_lat + dwgs_lat;
       var gcj = [ gcj_lon, gcj_lat ];
        return gcj;
    }
}

function transformlon(lon, lat)
{   
    var PI = Math.PI
    var ret = 300.0 + lon + 2.0 * lat + 0.1 * lon * lon + 0.1 * lon * lat + 0.1 * Math.sqrt(Math.abs(lon));
    ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lon * PI) + 40.0 * Math.sin(lon / 3.0 * PI)) * 2.0 / 3.0;
    ret += (150.0 * Math.sin(lon / 12.0 * PI) + 300.0 * Math.sin(lon / 30.0 * PI)) * 2.0 / 3.0;
    return ret;
}

function transformlat(lon, lat)
{   
    var PI = Math.PI
    var ret = -100.0 + 2.0 * lon + 3.0 * lat + 0.2 * lat * lat + 0.1 * lon * lat + 0.2 * Math.sqrt(Math.abs(lon));
    ret += (20.0 * Math.sin(6.0 * lon * PI) + 20.0 * Math.sin(2.0 * lon * PI)) * 2.0 / 3.0;
    ret += (20.0 * Math.sin(lat * PI) + 40.0 * Math.sin(lat / 3.0 * PI)) * 2.0 / 3.0;
    ret += (160.0 * Math.sin(lat / 12.0 * PI) + 320 * Math.sin(lat * PI / 30.0)) * 2.0 / 3.0;
    return ret;
}

//判断是否在国内,不在国内则不做偏移
function out_of_china(lon, lat)
{
    return (lon < 72.004 || lon > 137.8347) || ((lat < 0.8293 || lat > 55.8271) || false);
}